#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "prisoner.h"
#include "ga.h"
#include "population.h"
#include "genome.h"

using namespace std;

struct timeval begin_time;
struct timeval cur_time;

int MATRIXWIDTH = 0;
int **Payoff;

int bestchoice = 1;


void loadData(char *filename) 
{
	ifstream fp_in;
	fp_in.open( filename, std::ios::in);
	
	/** read the K of k*k matrix **/
	fp_in >> MATRIXWIDTH;
	int K = MATRIXWIDTH;
	
	/** prepare Payoff **/
	Payoff = new int*[K];
	for(int i = 0; i < K; i++)
		Payoff[i] = new int[K];

	/** read Payoff **/
	for(int i = 0; i < K; i++)
	{
		for(int j = 0; j < K; j++)
		fp_in >> Payoff[i][j];
	}

	int bestscore = 0;
	for(int i = 0; i < K; i++)
	{
		for(int j = 0; j < K; j++)
		{
			if(bestscore < Payoff[i][j])
			{
				bestscore = Payoff[i][j];
				bestchoice = i+1;
			}
		}
	}


	fp_in.close();
}



int main(int argc, char* argv[])
{
	/** mark begin time **/
	gettimeofday(&begin_time,NULL);


	/** load input Data (matrix) **/
	if(argc > 1)
		loadData(argv[1]);
	else
		loadData("defaultpayoffmatrix");



	/** do the evolution **/
	Population protagonist(50);
	Population antagonist(50);	
	GA ga(protagonist, antagonist,550);
	Genome *best;

	best = ga.run();

	/** write to the output matrix **/
	FILE *fp = fopen("2008_21007.str","w");
	best->print(fp);

	fclose(fp);

	return 0;
}
